/**
 *  *2010-8-16 下午04:46:58  add 
 */
package com.base.myproject.client.HR.batchpro;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.HR.bcode.BcodeSelectDialog;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 *2010-8-16
 * javazgw@gmail.com
 */
public class ChangeBcodeValue extends ProcItem {
	boolean isinit = false;
	BcodeSelectDialog<TreeNodeModelData> bsd_add = null;
	ArrayList<Column> columnlist = new ArrayList<Column>();
	LayoutContainer lc = new LayoutContainer();
	Button save = new Button("保存");
	SimpleComboBox com = new SimpleComboBox();
	ColumnModel cm;
	LabelField mark_l = new LabelField("值改变为：");
	TextField<String> mark_t = new TextField<String>(); 
	public ChangeBcodeValue() {
		this(null);
	}
	public ChangeBcodeValue(String name)
	 {
		 
		 super(name);
		init();
	 }
	private void init() {
		initColumn();
	}

	@Override
	public Widget initwidget() {
		if(!isinit)
		{
		HorizontalPanel hpanel = new HorizontalPanel();
		createGrid();
		LayoutContainer lc = initProButtons();
		
		hpanel.add(grid);
		hpanel.add(lc);
		this.add(hpanel);
		isinit = true;
		Tools.setResize(grid, 0.7,0.95);
		Tools.setResize(lc, 0.3,0.95);
		}
		return this;
	}
	
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		for(Column column:columnlist)
		{
			type.addField(column.getName());
		}
		
		
		
		StoreInfo info = new StoreInfo();
		info.setTablename("bcode_job");
		info.setKeyname(new String[] { "icode" });
	//	info.setSql("select contractstartdate,contractsenddate from bcode_job");
		info.setNeedAllRow("true");
		info.setForcenolimit("true");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info,false);
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C3 = new ColumnConfig("icode",
//				"icode", 180);
//		columns.add(C3);
		
		ColumnConfig C1 = new ColumnConfig("bcode",
				"人员", 100);

		columns.add(C1);
	
		for(Column column:columnlist)
		{
			
			ColumnConfig C_temp = new ColumnConfig(column.getName(),
					column.getTitle(), 100);
			C_temp.setHidden(column.isHide());
			columns.add(C_temp);
		}
		
		 cm = new ColumnModel(columns);
	
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SIMPLE);
		grid.setSelectionModel(cbs);
		return grid;
	}
	
	LayoutContainer initProButtons()
	{
	
		TableLayout tl = new TableLayout(2);
		lc.setLayout(tl);
		
		TableData td1 = new TableData();
		td1.setColspan(1);
		TableData td2 = new TableData();
		td2.setColspan(2);
		
		 bsd_add = new BcodeSelectDialog<TreeNodeModelData>(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				if(modeldatalist.size()>0)
				{
					this.setEmptyText("已经选择了数据");
					
					List<TreeNodeModelData>  selectchecklist=bsd_add.getTree().getCheckedSelection();
					
					if(selectchecklist.size()<=0)
					{
						MessageBox.alert("错误", "请选择“离职人员”", null	);
						return;
					}
					String bcodestr = "";
					for(TreeNodeModelData tnmd:selectchecklist)
					{
						BusiObject bo = new BusiObject();
						
						bcodestr +=" systembcode like '"+tnmd.get("systembcode")+"%25' or ";

					}
					bcodestr +=" 1=2";
					GWT.log("bcodestr ="+bcodestr);
					
					
					//String sql  = "select icode, bcode,employstatus,leavereason,leavemark,Resignationdate from bcode_job where exists (select * from systembcode_view where systembcode_view.bcode = bcode_job.bcode  and ( "+bcodestr+"))";
					String sql =GetSql();
					sql +="where exists (select * from systembcode_view where systembcode_view.bcode = bcode.bcode  and ( "+bcodestr+"))";
					GWT.log("sql ="+sql);
					store.getStoreinfo().setSql(sql);
					store.reload();
				}
			}

		};
		bsd_add.setNeesreselect(false);
		bsd_add.setEmptyText("未选择数据");
		bsd_add.setExpanded();
		bsd_add.setCheckNodes(CheckNodes.BOTH);
		
		
//		Resignationdate = new DateField();
//		 Resignationdate.getPropertyEditor().setFormat(
//					DateTimeFormat.getFormat("yyyy-MM-dd"));
		
		save.addListener(Events.OnClick,  getListener());
		lc.add(new LabelField("选择人员："),td1);
		lc.add(bsd_add,td1);
		
		
		for(Column column:columnlist)
		{
			com.add(column.getTitle());
		}
		
		
		lc.add(new LabelField("内容："),td1);
		lc.add(com,td1);
		lc.add(mark_l,td1);
		lc.add(mark_t,td1);
//		
//		lc.add(leavereason_l,td1);
//		lc.add(C_num9,td1);
//		
//		lc.add(resignationdate_l,td1);
//		lc.add(Resignationdate,td1);
//		
//		lc.add(mark_l,td1);
//		lc.add(mark_t,td1);
//		
//		leavereason_l.setVisible(false);
//		C_num9.setVisible(false);
//		mark_l.setVisible(false);
//		mark_t.setVisible(false);
//		resignationdate_l.setVisible(false);
//		Resignationdate.setVisible(false);
//		
		lc.add(save,td2);
//		
//		bsd_add.addListener(Events.Changed, getListener());
//		C_num8.addListener(Events.Changed, getListener());
//		C_num9.addListener(Events.Changed, getListener());
		mark_t.addListener(Events.Changed, getListener());
//		Resignationdate.addListener(Events.Changed, getListener());
		com.addListener(Events.Changed, getListener());
		return lc;
	}
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.OnClick) {                     
							if (be.getSource().equals(save)) {     
								store.trySave();
							}                                                      
						} else if (be.getType() == Events.Changed) {  
							if (be.getSource().equals(mark_t)) {
								int i = 0;
								String key =null;
								for(Column column:columnlist)
								{
									if(column.getTitle().equals(com.getSimpleValue()))
									{
										key = column.getName();
										break;
									}
								}
								
								if(key!=null)
								for(BusiObject bo:getStore().getModels())
								{
								
										grid.setValue(i,key,mark_t.getValue() );
									i++;
								}
							}
							
							else if (be.getSource().equals(com)) {
								
								String value = com.getSimpleValue()+"";
								for(Column column:columnlist)
								{
									
									
									if(value.equals(column.getTitle()))
									{
										
										grid.getColumnModel().setHidden(cm.getIndexById(column.getName()),false);
										//cm.getColumnById(column.getName()).setHidden(false);
									}
									else
									{
										grid.getColumnModel().setHidden(cm.getIndexById(column.getName()),true);
									}
								}
							
							}     
							
					
							
							
							
							
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}     
	
	void initColumn()
	{
		
		
		addColumn("hr_idcard_addr","身份证地址",true);
		addColumn("hr_Marital_status","婚姻状况",true);
		addColumn("hr_Census_Province","户籍省份",true);
		addColumn("hr_Census_city","户籍市",true);
		addColumn("hr_Census_area","户籍区",true);
		addColumn("hr_social_no","社保编号",true);
		addColumn("hr_bankno","银行",true);
		addColumn("hr_bankcard","银行账户",true);

	}
	
	String GetSql()
	{
		String sql = " select icode, ";
		for(Column column:columnlist)
		{
			sql += column.getName()+",";
		}
		sql +=" bcode from bcode ";
		return sql;
	}
	
	void addColumn(Column column)
	{
		columnlist.add(column);
	}
	void addColumn(String name ,String title,boolean hide)
	{
		Column column = new Column(name,title,hide);
		addColumn(column);
	}

	Column getColumn(String name)
	{
		for(Column column:columnlist)
		{
			if(column.getName().equals(name))
				return column;
		}
		return null;
	}
	class Column
	{
		String name,title;
		boolean hide;
		public Column(String name ,String title,boolean hide)
		{
			this.name = name;
			this.title = title;
			this.hide = hide;
		}
		public boolean isHide() {
			return hide;
		}
		public void setHide(boolean hide) {
			this.hide = hide;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		
		
		
	}
}
